package com.wx.mythread.jmm;

import java.util.concurrent.CountDownLatch;

/**
 * 演示重排序
 * 直达达到某个条件才停止，测试小概率事件
 */
public class OutOfOrderExcution {
    private volatile static int x = 0, y = 0;
    private volatile static int a = 0, b = 0;

    public static void main(String[] args) throws InterruptedException {
        int i = 0;
        for (;;){
            i++;
            x=0;
            y=0;
            a=0;
            b=0;

            CountDownLatch latch = new CountDownLatch(1);

            Thread one = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    a = 1;
                    x = b;
                }
            });

            Thread two = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        latch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    b = 1;
                    y = a;
                }
            });

            one.start();
            two.start();
            latch.countDown();
            one.join();
            two.join();

            // if (x == 1 && y == 1){
            if (x == 0 && y == 0){
                System.out.println("i="+i+", x="+x+", y="+y);
                break;
            }else {
                System.out.println("i="+i+", x="+x+", y="+y);
            }

        }
    }

}
